package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.StringUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/15 20:22
 * E-Mail Address：16601203965@163.com
 */
public class SearchItermAdapter extends RecyclerView.Adapter<SearchItermAdapter.SearchItemViewHolder> {

    private static final int TYPE_MORE = 1;
    private static final int TYPE_NORMAL = 2;

    private Context context;
    private List<ItemSearchBean.SubBean> dataList = new ArrayList<>();
    private List<ItemSearchBean.SubBean> oldDataList = new ArrayList<>();//保存原来顺序
    private int COUNT_FOLD = 3;
    private boolean isFold = true;
    private SearchItemView searchItemView;
    private boolean isMultiSelected = false;
    private boolean endMoreShow = false;

    public void setEndMoreShow(boolean endMoreShow) {
        this.endMoreShow = endMoreShow;
    }

    public void setMultiSelected(boolean multiSelected) {
        isMultiSelected = multiSelected;
    }

    public SearchItermAdapter(Context context, SearchItemView searchView) {
        this.context = context;
        this.searchItemView = searchView;
    }

    public void setDataList(List<ItemSearchBean.SubBean> dataList) {
        this.dataList = dataList;
        this.oldDataList = dataList;

    }

    public SearchItermAdapter(Context context, List<ItemSearchBean.SubBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.oldDataList = dataList;
    }

    @NonNull
    @Override
    public SearchItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_search, viewGroup, false);
        return new SearchItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final SearchItemViewHolder holder, final int position) {

        ItemSearchBean.SubBean searchBean = dataList.get(position);

        if (TYPE_NORMAL == getItemViewType(position)) {
            holder.tv_name.setText(searchBean.getName());
            holder.tv_name.setSelected(searchBean.isChecked());
            if (searchBean.isChecked()) {
                Logger.i("----------------------dd:" + searchItemView.itemSearchBean.getId());
                if (searchItemView.itemSearchBean.getId() == 95) {//出版社-图书
                    holder.tv_name.setBackgroundResource(R.mipmap.tv_selected_book);
                } else if (searchItemView.itemSearchBean.getId() == 32) {//软件平台载体
                    holder.tv_name.setBackgroundResource(R.mipmap.tv_selected_software);
                } else {
                    holder.tv_name.setBackgroundResource(R.drawable.tv_selected);
                }

                holder.tv_name.setTextColor(ContextCompat.getColor(context, R.color.color_8ed9f1));
            } else {
                holder.tv_name.setBackgroundResource(R.drawable.shape_r2_item);
                holder.tv_name.setTextColor(ContextCompat.getColor(context, R.color.color_ff333333));
            }
//            holder.tv_name.setTextColor(ContextCompat.getColor(context,R.color.color_ff333333));
            holder.itemView.setOnClickListener(new SearchItermClickListener(searchBean, position));
        } else {
            holder.tv_name.setText("查看全部 >");
            holder.itemView.setOnClickListener(new MoreClickListener(searchBean, position));
        }

        setParentViewSelectValue();//重置SearchItemView右边箭头地方选中展示的文字

    }

    @Override
    public int getItemCount() {
        if (isFold) {
            if (dataList.size() >= COUNT_FOLD) {
                return COUNT_FOLD;
            } else {
                return dataList.size();
            }
        } else {
            return dataList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (endMoreShow && isFold && position == COUNT_FOLD - 1) {
            return TYPE_MORE;
        } else {
            return TYPE_NORMAL;
        }
    }

    public void openCloseStatus(boolean isFold) {
        this.isFold = isFold;
        notifyDataSetChanged();
    }


    //查看更多
    class MoreClickListener implements View.OnClickListener {

        ItemSearchBean.SubBean searchBean;
        int position;

        public MoreClickListener(ItemSearchBean.SubBean searchBean, int position) {
            this.searchBean = searchBean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (moreInterface != null) {
                moreInterface.moreClick();
            }
        }
    }


    class SearchItermClickListener implements View.OnClickListener {

        ItemSearchBean.SubBean searchBean;
        int position;

        public SearchItermClickListener(ItemSearchBean.SubBean searchBean, int position) {
            this.searchBean = searchBean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (!isMultiSelected) {
                for (int index = 0; index < dataList.size(); index++) {
                    if (index != position) {
                        dataList.get(index).setChecked(false);
                    }
                }
            }
            searchBean.setChecked(!searchBean.isChecked());


            //塌缩   add 2019-09-20
            if (searchItemView.itemSearchBean.isCollapseEnable()){
                //2019-09-19  让选中的放在第一个
                List<ItemSearchBean.SubBean> tempDataList = new ArrayList<>();
                if (searchBean.isChecked()) {
                    tempDataList.add(searchBean);
                    for (ItemSearchBean.SubBean tempBean : dataList) {
                        if (tempBean.getId() != searchBean.getId()) {
                            tempDataList.add(tempBean);
                        }
                    }
                    dataList = tempDataList;
                    isFold = true;
                } else {
                    isFold = false;
                    dataList = oldDataList;
                }
            }


//            notifyItemChanged(position);
            notifyDataSetChanged();
            setParentViewSelectValue();


            //塌缩   add 2019-09-20
            if (searchItemView.itemSearchBean.isCollapseEnable()){
                if (searchBean.isChecked()) {
                    if (itemSelectedInterface != null) {
                        itemSelectedInterface.selectItem(searchBean);
                    }
                }
            }



            //清空时间控件
            searchItemView.clearTimeWidget();
        }
    }

    private void setParentViewSelectValue() {
        String selectStr = "";
        String selectId = "";
        for (ItemSearchBean.SubBean searchBean : dataList) {
            if (searchBean.isChecked()) {
                selectStr = selectStr + searchBean.getName() + ",";
                selectId = selectId + searchBean.getId() + ",";
            }
        }
        if (!StringUtils.isEmpty(selectStr)) {
            selectStr = selectStr.substring(0, selectStr.length() - 1);
            if (selectId.length() > 0) {
                selectId = selectId.substring(0, selectId.length() - 1);
            }
        } else {
            selectStr = "全部";
        }
        searchItemView.tv_show_select.setText(selectStr);
        if (!"全部".equalsIgnoreCase(selectStr)) {
            searchItemView.itemSearchBean.setValue(selectStr);//让每组itemSearchBean的value属性维护选中的值
            searchItemView.itemSearchBean.setIdValue(selectId);
        } else {
            searchItemView.itemSearchBean.setValue(ConstantsUtils.EMPTY_STR);
        }


    }

    /**
     * 更新数据显示group右侧选择
     */
    public void notifyDataChangeShowSelect() {
        notifyDataSetChanged();
        setParentViewSelectValue();
    }


    public void clearTabStatus() {
        for (ItemSearchBean.SubBean searchBean : dataList) {
            searchBean.setChecked(false);
        }
        searchItemView.tv_show_select.setText("全部");
        notifyDataSetChanged();
    }


    class SearchItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public SearchItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }

    public void addItemNums(int num) {
        COUNT_FOLD = num;
    }


    private MoreInterface moreInterface;

    public void setMoreInterface(MoreInterface moreInterface) {
        this.moreInterface = moreInterface;
    }

    public interface MoreInterface {
        public void moreClick();
    }

    private ItemSelectedInterface itemSelectedInterface;

    public void setItemSelectedInterface(ItemSelectedInterface itemSelectedInterface) {
        this.itemSelectedInterface = itemSelectedInterface;
    }

    public interface ItemSelectedInterface {
        public void selectItem(ItemSearchBean.SubBean selectSubBean);
    }


}
